package com.woniu.tms.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.tms.entity.Role;
import com.woniu.tms.manage.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 跳转分配角色页面
     *
     * @param uid
     * @param model
     * @return
     */
    @RequestMapping("assignRole")
    public String assignRole(Integer uid, Model model) {
        model.addAttribute("uid", uid);
        return "WEB-INF/manage/role/assignRole";
    }

    /**
     * 展示角色列表
     *
     * @param uid
     * @return
     */
    @RequestMapping("assignRoleShow")
    @ResponseBody
    public Map<String, Object> assignRoleShow(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Role> unAssignRoleList = roleService.unAssignRoleList(uid);
            List<Role> assignRoleList = roleService.assignRoleList(uid);
            map.put("unAssignRoleList", unAssignRoleList);
            map.put("assignRoleList", assignRoleList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 跳转角色index
     *
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "WEB-INF/manage/role/index";
    }

    /**
     * 查询角色列表分页
     *
     * @param pageNum
     * @param pageSize
     * @param condition
     * @return
     */
    @RequestMapping("indexDo")
    @ResponseBody
    public Map<String, Object> indexDo(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       String condition) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Role> roleList = roleService.getByCondition(condition);
            PageInfo<Role> pageInfo = new PageInfo<>(roleList);
            map.put("roleList", roleList);
            map.put("pageInfo", pageInfo);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 角色删除
     *
     * @param rid
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(Integer rid) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.delete(rid);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 批量删除
     *
     * @param rids
     * @return
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Map<String, Object> delAll(Integer[] rids) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.delAll(rids);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 跳转增加角色页面
     *
     * @return
     */
    @RequestMapping("add")
    public String add() {
        return "WEB-INF/manage/role/add";
    }

    /**
     * 增加角色功能
     *
     * @param role
     * @return
     */
    @RequestMapping("addDo")
    @ResponseBody
    public Map<String, Object> addDo(Role role) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.add(role);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 跳转修改角色页面
     *
     * @return
     */
    @RequestMapping("update")
    public String update(Integer rid, Model model) {
        Role role = roleService.selectByRid(rid);
        model.addAttribute("role", role);
        return "WEB-INF/manage/role/update";
    }


    /**
     * 修改角色按钮
     *
     * @param role
     * @return
     */
    @RequestMapping("updateDo")
    @ResponseBody
    public Map<String, Object> updateDo(Role role) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.update(role);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }
}
